/*
 * Copyright (c) 2013 WDCY Information Technology Co. Ltd
 * www.wdcy.cc
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * WDCY Information Technology Co. Ltd ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement you
 * entered into with WDCY Information Technology Co. Ltd.
 */
package com.andaily.zhishifenzi.service.impl;

import com.andaily.zhishifenzi.domain.commons.CommonsRepository;
import com.andaily.zhishifenzi.domain.commons.GeckoFile;
import com.andaily.zhishifenzi.domain.commons.Photo;
import com.andaily.zhishifenzi.domain.dto.album.PhotoDto;
import com.andaily.zhishifenzi.domain.dto.file.GeckoFileDto;
import com.andaily.zhishifenzi.domain.player.Player;
import com.andaily.zhishifenzi.domain.player.PlayerRepository;
import com.andaily.zhishifenzi.service.GeckoFileService;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * @author Shengzhao Li
 */
@Service("geckoFileService")
public class GeckoFileServiceImpl implements GeckoFileService {

    @Autowired
    private CommonsRepository commonsRepository;
    @Autowired
    private PlayerRepository playerRepository;

    @Override
    @Cacheable(value = "geckoFileCache")
    public GeckoFileDto loadFileByGuid(String guid) {
        GeckoFile geckoFile = commonsRepository.findGeckoFileByGuid(guid);
        return new GeckoFileDto(geckoFile, true);
    }

    @Override
    public PhotoDto loadPlayerPhotoByGuid(String guid) {
        if (StringUtils.isEmptyOrWhitespaceOnly(guid)) {
            return null;
        }
        Player player = playerRepository.findByGuid(guid);
        Photo photo = player.headPhoto();
        return photo != null ? new PhotoDto(photo, true) : null;
    }

}